//激活订单
function GetActivationState() {
    debugger;
    var orderStatus = Xrm.Page.getAttribute("new_status").getValue();
    if (orderStatus && orderStatus == "100000000") {
        var customers = Xrm.Page.getAttribute("customerid").getValue();
        if (customers && customers.length > 0) {
            var customerid = customers[0].id;
            var accountres = XrmServiceToolkit.Soap.Retrieve("account", customerid, ["creditlimit"]);
            var creditlimit = 0
            if (accountres &&
                accountres.attributes["creditlimit"] &&
                accountres.attributes["creditlimit"].value) {
                creditlimit = accountres.attributes["creditlimit"].value;
            } else {
                Xrm.Utility.confirmDialog("此客户信用额度没有给值！是否还要激活",
                   function () {
                       Xrm.Page.getAttribute("new_status").setValue(100000003);
                       Xrm.Page.data.save().then(function () {
                           if (window.parent) window.parent.location.reload(true);
                           else window.location.reload(true);
                       },
                       function () {
                           Xrm.Utility.alertDialog("提交失败！");
                       });

                   },
					function () {
					    
					});
                return;
            }

            var fetchXml = "<fetch distinct='false' mapping='logical' aggregate='true' >" +
                                "  <entity name='new_creconsdetail' >" +
                                "    <attribute name='new_quota' alias='sumquota' aggregate='sum' />" +
                                "    <filter>" +
                                "      <filter type='and' >" +
                                "        <condition attribute='statecode' operator='eq' value='0' />" +
                                "        <condition attribute='statuscode' operator='eq' value='1' />" +
                                "        <condition attribute='new_account' operator='eq' value='" + customerid + "' />" +
                                "      </filter>" +
                                "    </filter>" +
                                "  </entity>" +
                                "</fetch>";
            var totalQuota = 0;
            var retrTotalQuota = XrmServiceToolkit.Soap.Fetch(fetchXml);

            if (retrTotalQuota && retrTotalQuota.length > 0) {
                if (retrTotalQuota[0].attributes["sumquota"].value)
                    totalQuota = parseFloat(retrTotalQuota[0].attributes["sumquota"].value);
            }
            if ((creditlimit + totalQuota) < 0) {
                Xrm.Utility.confirmDialog("客户剩余信用已经为负值,是否还要提交",
                    function () {
                        Xrm.Page.getAttribute("new_status").setValue(100000003);
                        Xrm.Page.data.save().then(function () {
                            if (window.parent) window.parent.location.reload(true);
                            else window.location.reload(true);
                        },
                    function () {
                        Xrm.Utility.alertDialog("提交失败！");
                    });
                    },
					function () {
					    console.log("cancel");
					});
            } else {
                Xrm.Utility.confirmDialog("客户信用原始额度：" + creditlimit + " 客户剩余用信用额度" + (creditlimit + totalQuota),
					function () {
					    Xrm.Page.getAttribute("new_status").setValue(100000003);
					    Xrm.Page.data.save().then(function () {
					        if (window.parent) window.parent.location.reload(true);
					        else window.location.reload(true);
					    },
                        function () {
                            Xrm.Utility.alertDialog("提交失败！");
                        });

					},
					function () {
					    console.log("cancel");
					});
            }
        }
    } else {
        Xrm.Utility.alertDialog("订单在此状态下不允许激活");
        return;
    }

}

